<template>
  <div class="wrap_menu">
    <el-form ref="formData" :rules="rules" :model="formData" class="form-wrap" label-position="top" label-width="80px">
      <el-form-item :label="$t('configLazyLoad.process.nodeName')" prop="DisplayName">
        <el-input v-model.trim="formData.DisplayName"></el-input>
      </el-form-item>
      <el-form-item label="欢迎词语音文件：">
        <el-select v-model="formData.Welfilename" placeholder="请选择" style="width: 100%;">
          <el-option
            label="请选择"
            value="">
          </el-option>
          <el-option
            v-for="item in ivr_sound.ivr_sound"
            v-if="item.SoundType == 'normal'"
            :label="item.DisplayName"
            :key="item._id"
            :value="item.Filename">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="无效输入语音文件：">
        <el-select v-model="formData.Invalidfilename" placeholder="请选择" style="width: 100%;">
          <el-option
            label="请选择"
            value="">
          </el-option>
          <el-option
            v-for="item in ivr_sound.ivr_sound"
            v-if="item.SoundType == 'normal'"
            :label="item.DisplayName"
            :key="item._id"
            :value="item.Filename">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="输入超时语音文件：">
        <el-select v-model="formData.Timeoutfilename" placeholder="请选择" style="width: 100%;">
          <el-option
            label="请选择"
            value="">
          </el-option>
          <el-option
            v-for="item in ivr_sound.ivr_sound"
            v-if="item.SoundType == 'normal'"
            :label="item.DisplayName"
            :key="item._id"
            :value="item.Filename">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="错误结束语音文件：">
        <el-select v-model="formData.Byefilename" placeholder="请选择" style="width: 100%;">
          <el-option
            label="请选择"
            value="">
          </el-option>
          <el-option
            v-for="item in ivr_sound.ivr_sound"
            v-if="item.SoundType == 'normal'"
            :key="item._id"
            :label="item.DisplayName"
            :value="item.Filename">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="评价结束语音文件：">
        <el-select v-model="formData.Endfilename" placeholder="请选择" style="width: 100%;">
          <el-option
            label="请选择"
            value="">
          </el-option>
          <el-option
            v-for="item in ivr_sound.ivr_sound"
            v-if="item.SoundType == 'normal'"
            :key="item._id"
            :label="item.DisplayName"
            :value="item.Filename">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="验证变量名：">
        <el-select v-model="formData.Data" placeholder="请选择" style="width: 100%;">
          <el-option
            v-for="item in vars.vars"
            :key="item._id"
            :label="item.VarName"
            :value="item._id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="正确完成评价后转向：">
        <node-select-item :curPbx="curPbx" ref="nodeSelect" :ToPeerType="formData.NextPeerType" :ToPeer="formData.NextPeer"></node-select-item>
      </el-form-item>
      <div class="edit-ivr-wrap" style="border: 1px solid #E7E7EB;" v-if="!form.ParentId && supportLevel2">
        <el-row style="padding: 6px 12px 4px 12px;background-color: #F4F5F9;">
          <el-col :span="11">按键</el-col>
          <el-col :span="11">流转到</el-col>
          <el-col :span="2"><i class="el-icon-circle-plus" style="color: #409EFF;font-size: 18px;" @click="addKeyList"></i></el-col>
        </el-row>
        <div>
          <el-row style="padding: 6px 12px 14px 12px;border-bottom: 1px solid #E7E7EB;" v-for='(item, index) in keyList'>
            <el-col :span="11">
              <el-row style="padding-top: 8px;">
                <el-col>
                  <el-select placeholder="请选择" size="mini" style="width: 90%;" v-model="item.key">
                    <el-option
                      v-for="one in keyArr"
                      :label="one"
                      :value="one">
                    </el-option>
                  </el-select>
                </el-col>
              </el-row>
            </el-col>
            <el-col :span="11">
              <el-row style="padding-top: 8px;">
                <el-col>
                  <el-select placeholder="请选择" size="mini" style="width: 90%;" v-model = 'item.value'>
                    <el-option
                      v-for="peer in usablePeerList"
                      :label="peer.DisplayName"
                      :value="peer.Exten">
                    </el-option>
                  </el-select>
                </el-col>
              </el-row>
            </el-col>
            <el-col :span="2"><i @click = 'delKeyList(index)' class="el-icon-remove" style="color: #FF6B6B;font-size: 18px;padding-top:14px"></i></el-col>
          </el-row>
        </div>
      </div>
    </el-form>
    <div class="footer">
      <el-button class='slide-box__button' @click='cancelClick'>
        取消
      </el-button>
      <el-button class='slide-box__button' type="primary" @click='saveClick'>
        保存
      </el-button>
    </div>
  </div>
</template>
<script>
  import nodeSelectItem from '../../base/nodeSelectItem.vue'
  import {deepClone} from 'src/utils/m7Utils.js'
  export default {
    name: 'satisfactionDeal',
    data () {
      return {
        formData: { // 自定义满意度节点
          _id: '',
          DisplayName: '',
          Exten: '',
          Welfilename: '',
          Invalidfilename: '',
          Timeoutfilename: '',
          Byefilename: '',
          Endfilename: '',
          Data: '',
          NextPeerType: '',
          NextPeer: ''
        },
        rules: {
          DisplayName: [
            { required: true, message: this.$t('configLazyLoad.process.processLabel7'), trigger: 'blur' }
          ]
        },
        keyList: []
      }
    },
    computed: {
      curPbx () {
        return this.$store.state.config.callCenter.currentPBX
      },
      Account () {
        return this.$store.state.session.user.account
      },
      ivr_sound () {
        return this.$store.state.session.ivrMap[this.curPbx].data
      },
      vars () {
        return this.$store.state.session.ivrMap[this.curPbx].data
      },
      usablePeerList () {
        return this.form.children ? this.form.children : []
      },
      keyArr () {
        let keyArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
        let arr = this.keyList
        let newKeyArr = keyArr.filter((item) => {
          let flag = true
          arr.forEach((keyItem) => {
            let key = keyItem.key
            if (key === item) {
              flag = false
            }
          })
          return flag
        })
        return newKeyArr
      },
      supportLevel2 () {
        return this.$store.state.cti.globalLet.ass_current_new_features
      }
    },
    components: {
      nodeSelectItem
    },
    props: {
      form: {
        type: Object,
        default: function () {
          return {
            _id: '',
            DisplayName: '',
            Exten: '',
            Welfilename: '',
            Invalidfilename: '',
            Timeoutfilename: '',
            Byefilename: '',
            Endfilename: '',
            Data: '',
            NextPeerType: '',
            NextPeer: ''
          }
        }
      },
      supportLevel2: {
        type: Boolean,
        default: false
      }
    },
    methods: {
      initForm (obj) {
        this.formData = deepClone(obj)
        this.formData.Welfilename = ''
        this.formData.Byefilename = ''
        this.formData.Invalidfilename = ''
        this.formData.Timeoutfilename = ''
        this.formData.Endfilename = ''
//        let Welfilenameflag = false
//        let Invalidfilenameflag = false
//        let Timeoutfilenameflag = false
//        let Byefilenameflag = false
//        let Endfilenameflag = false
        if (this.ivr_sound.ivr_sound && this.ivr_sound.ivr_sound.length > 0) {
          this.ivr_sound.ivr_sound.forEach(item => {
            if (this.form.Welfilename === item.Filename) {
              this.formData.Welfilename = item.Filename
            }
            if (this.form.Invalidfilename === item.Filename) {
              this.formData.Invalidfilename = item.Filename
            }
            if (this.form.Timeoutfilename === item.Filename) {
              this.formData.Timeoutfilename = item.Filename
            }
            if (this.form.Byefilename === item.Filename) {
              this.formData.Byefilename = item.Filename
            }
            if (this.form.Endfilename === item.Filename) {
              this.formData.Endfilename = item.Filename
            }
          })
        }
        // 清空按键信息
        this.keyList = []
        if (this.formData.KeysMap) {
          // let keyList = []
          let arr = this.formData.KeysMap.split(',')
          arr.forEach((item, index) => {
            let keyArr = item.split('=')
            let key = keyArr[0]
            let value = keyArr[1]
            this.usablePeerList.forEach((peerItem) => {
              if (peerItem.Exten === value) {
                this.keyList.push({key: key, value: value})
              }
            })
          })
        }
//        if (!Welfilenameflag) {
//          this.formData.Welfilename = ''
//        } else {
//          this.formData.Welfilename = this.form.Welfilename
//        }
      },
      cancelClick () {
        this.$emit('cancelClick')
      },
      // 更新pbx数据
      updateVuexPbx () {
        this.$store.dispatch('querySatisfaction', {Account: this.Account, PBX: this.curPbx}).then(res => {
          if (res && res.success) {
            this.$store.commit('SET_IVRMAP_DETAIL', {PBX: this.curPbx, typeName: 'ivr_node_satisfaction', data: res.list || [], flag: 'data', node: this})
          }
          this.$emit('saveClick')
        })
      },
      saveClick () {
        this.$refs['formData'].validate((valid) => {
          if (valid) {
            let form = deepClone(this.formData)
            if (!form.id) {
              form.Account = this.Account
              form.PBX = this.curPbx
            }
            form.NextPeerType = this.$refs.nodeSelect.ToPeerTypeThis
            form.NextPeer = this.$refs.nodeSelect.ToPeerThis
            form.UpdateTime && delete form.UpdateTime
            if (this.keyList && this.keyList.length > 0) {
              let str = ''
              this.keyList.forEach((item, index) => {
                if (index === (this.keyList.length - 1)) {
                  str += item.key + '=' + item.value
                } else {
                  str += item.key + '=' + item.value + ','
                }
              })
              form.SatisfactionGarde = 'parent'
              form.KeysMap = str
            } else {
              if (!form.ParentId) {
                delete form.SatisfactionGarde
                delete form.KeysMap
              }
            }
            if (form.children) {
              delete form.children
              delete form.Children
            }
            this.$store.dispatch('updateSatisfaction', form).then((res) => {
              if (res.success) {
                this.$message({
                  message: '保存成功',
                  type: 'success'
                })
                this.updateVuexPbx()
              } else {
                this.$message.error('保存失败')
              }
            })
          }
        })
      },
      addKeyList () {
        this.keyList.push({key: '', value: ''})
      },
      delKeyList (index) {
        this.keyList.splice(index, 1)
      }
    },
    beforeMount () {
      this.initForm(this.form)
    },
    watch: {
      form (cur, old) {
        this.$refs['formData'].resetFields()
        this.initForm(cur)
        this.$refs.nodeSelect.init()
      }
    }
  }
</script>
<style lang="stylus" scoped>
  @import "../../../../../../assets/common.styl"
  .note
    margin-top 20px
    padding 0 20px
    p
      color $c-prompt
    p.two
      margin-top 10px
  .wrap_menu
    width 100%
    padding-bottom 60px
    .form-wrap
      padding 20px
  .footer
    position absolute
    bottom 0
    left 0
    width 100%
    background rgba(255,255,255,1)
    box-shadow 0px -2px 4px 0px rgba(5,49,38,0.05)
    border-top 1px solid rgba(232,232,232,1)
    display flex
    justify-content center
    padding 13px 0
    box-sizing border-box
    .slide-box__button
      padding 8px 20px 8px
      box-sizing border-box
</style>
